#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
using namespace std;

template<class K>
struct BSTreeNode
{
	BSTreeNode<K>* _left;
	BSTreeNode<K>* _right;
	K _key;

	BSTreeNode(const K& key):_left(nullptr),_right(nullptr),_key(key){}
};

template<class K>
class BSTree
{
	typedef BSTreeNode<K> Node;
public:
	BSTree() :_root(nullptr) {}
	~BSTree(){}

	bool Insert(const K& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(key);
		if (parent->_key < key)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		return true;
	}

	int find(const K& key)
	{
		Node* cur = _root;
		int t = 1;
		while (cur)
		{
			if (cur->_key < key)
			{
				cur = cur->_right;
				++t;
			}
			else if (cur->_key > key)
			{
				cur = cur->_left;
				++t;
			}
			else
			{
				return t;
			}
		}
		return -1;
	}

	void Inorder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		Inorder(root->_left);
		cout << root->_key << " ";
		Inorder(root->_right);
	}

	Node* get_root()
	{
		return _root;
	}

private:
	Node* _root;
};


void test6()
{
	int t;
	cin >> t;
	while (t--)
	{
		BSTree<int> BST;
		int n, num;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> num;
			BST.Insert(num);
		}
		BST.Inorder(BST.get_root());
		cout << endl;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> num;
			int ret = BST.find(num);
			cout << ret << endl;
		}
	}
}

int main()
{
	test6();
	return 0;
}
